﻿using System;
using System.Text;
using System.Security.Cryptography;
using Battle.Net.Model;

namespace Battle.Net.Infrastructure
{
    internal static class Authentication
    {
        private static string CreateSignature(string date, string urlPath, AuthenticationKeys keys)
        {
            var dataToSign = String.Format("GET\n{0}\n{1}\n",
                                           date,
                                           urlPath);

            var alg = new HMACSHA1(Encoding.UTF8.GetBytes(keys.PrivateKey));
            var hash = alg.ComputeHash(Encoding.UTF8.GetBytes(dataToSign));
            var signature = Convert.ToBase64String(hash);

            return String.Format("BNET {0}:{1}", keys.PublicKey, signature);
        }

        internal static string CreateHeader(string url, AuthenticationKeys keys)
        {
            var dateString = DateTime.UtcNow.ToString("r");
            return CreateSignature(dateString, url, keys);
        }
    }
}
